define(["jquery", "easy-admin", "xm-select"], function ($, ea) {

    var init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTableRenderId',
        index_url: 'mall.shop/index',
        add_url: 'mall.shop/add',
        edit_url: 'mall.shop/edit',
        delete_url: 'mall.shop/delete',
        export_url: 'mall.shop/export',
        modify_url: 'mall.shop/modify',
        asyncMachine_url: 'mall.shop/machine',
        print_url: 'mall.shop/print'
    };

    var Controller = {

        index: function () {
            ea.table.render({
                init: init,
                toolbar: ['refresh',
                    [{
                        text: '添加',
                        url: init.add_url,
                        method: 'open',
                        auth: 'add',
                        class: 'layui-btn layui-btn-normal layui-btn-sm',
                        icon: 'fa fa-plus ',
                        extend: 'data-full="true"',
                    }, {
                        text: '总店打印机',
                        url: init.print_url,
                        method: 'open',
                        auth: 'print',
                        class: 'layui-btn layui-btn-normal layui-btn-sm',
                        icon: 'fa fa-plus ',
                        extend: 'data-full="true"',
                    }],
                    'delete', 'export'],
                cols: [[
                    {type: 'checkbox'},
                    {field: 'id', title: 'id'},
                    {field: 'title', title: '店铺名称', minWidth: 100},
                    {field: 'machine_id', title: '商超打印机', minWidth: 120},
                    {field: 'express_id', title: '驿站打印机', minWidth: 120},
                    {field: 'logo', title: '店铺logo', templet: ea.table.image, search: false, width: 120},
                    {field: 'address', title: '店铺地址', minWidth: 200},
                    {field: 'lat', title: '店铺纬度', search: false, minWidth: 180},
                    {field: 'lng', title: '店铺经度', search: false, minWidth: 180},
                    {field: 'sort', title: '排序', edit: 'text', search: false},
                    {
                        field: 'status',
                        search: 'select',
                        selectList: ["禁用", "启用"],
                        title: '状态',
                        templet: ea.table.switch,
                        width: 120
                    },
                    {field: 'create_time', title: '创建时间', search: 'range', width: 180},
                    {
                        width: 250, title: '操作', templet: ea.table.tool, operat: [
                            [{
                                text: '授权打印机',
                                url: init.asyncMachine_url,
                                method: 'open',
                                auth: 'machine',
                                class: 'layui-btn layui-btn-xs layui-btn-normal',
                            }, {
                                text: '编辑',
                                extra: 'name',
                                url: init.edit_url,
                                method: 'open',
                                auth: 'edit',
                                class: 'layui-btn layui-btn-xs layui-btn-success',
                                extend: 'data-full="true"',
                            }],
                            'delete']
                    },
                ]],
            });

            ea.listen();
        },
        add: function () {
            let province = xmSelect.render({
                el: '#province',
                toolbar: {
                    show: true,
                },
                filterable: true,
                paging: true,
                pageSize: 10,
                data: [],
                radio: true,
                clickClose: true,
                name: 'province',
                on: function (data) {
                    console.log(data)
                    let arr = data.arr,
                        isAdd = data.isAdd;
                    if (isAdd) {
                        $.get("/admin/system.city/getProvinceByCityNameAndValue", {
                            province: arr[0].value
                        }).then(res => {
                            if (res.code == 1) {
                                city.update({
                                    data: res.data
                                })
                            }
                        })
                    }
                }
            })
            let city = xmSelect.render({
                el: '#city',
                toolbar: {
                    show: true,
                },
                filterable: true,
                paging: true,
                pageSize: 10,
                data: [],
                radio: true,
                clickClose: true,
                tips: '请选择省',
                name: 'city',
                on: function (data) {
                    let arr = data.arr,
                        isAdd = data.isAdd;
                    if (isAdd) {
                        $.get("/admin/system.area/getCityByAreaNameAndValue", {
                            city: arr[0].value
                        }).then(res => {
                            if (res.code == 1) {
                                area.update({
                                    data: res.data
                                })
                            }
                        })
                    }
                }
            })
            let area = xmSelect.render({
                el: '#area',
                toolbar: {
                    show: true,
                },
                filterable: true,
                paging: true,
                pageSize: 10,
                data: [],
                radio: true,
                clickClose: true,
                tips: '请选择市',
                name: 'area',
                on: function (data) {
                    console.log(data)
                }
            })
            let address = xmSelect.render({
                el: '#address',
                toolbar: {
                    show: true,
                },
                filterable: true,
                paging: true,
                pageSize: 10,
                data: [],
                radio: true,
                clickClose: true,
                tips: '请输入搜索',
                name: 'address',
                remoteSearch: true,
                remoteMethod: function (val, cb, show) {
                    //这里如果val为空, 则不触发搜索
                    if (!val) {
                        return cb([]);
                    }
                    let obj_pro = xmSelect.get('#province', true);

                    //这里引入了一个第三方插件axios, 相当于$.ajax
                    $.get("/admin/mall.shop/getShopByAddress", {
                        keywords: val,
                        city: obj_pro.getValue('nameStr')
                    }).then(res => {
                        if (res.code === 1) {
                            console.log(res.data)
                            cb(res.data)
                        } else {
                            layer.msg(res.msg)
                        }
                    })
                },
                on: function (data) {
                    console.log(data)
                    let arr = data.arr,
                        isAdd = data.isAdd;
                    if (isAdd) {
                        if (arr.length != 0) {
                            $("#lat").val(arr[0].lat);
                            $("#lng").val(arr[0].lng);
                        }
                    }
                }

            })
            $.get("/admin/system.province/getProvinceByList").then(res => {
                if (res.code === 1) {
                    province.update({
                        data: res.data
                    })
                }
            })
            ea.listen();
        },
        edit: function () {
            let province = xmSelect.render({
                el: '#province',
                toolbar: {
                    show: true,
                },
                filterable: true,
                paging: true,
                pageSize: 10,
                data: [],
                radio: true,
                clickClose: true,
                name: 'province',
                on: function (data) {
                    console.log(data)
                    let arr = data.arr,
                        isAdd = data.isAdd;
                    if (isAdd) {
                        $.get("/admin/system.city/getProvinceByCityNameAndValue", {
                            province: arr[0].value
                        }).then(res => {
                            if (res.code == 1) {
                                city.update({
                                    data: res.data
                                })
                            }
                        })
                    }
                }
            })
            let city = xmSelect.render({
                el: '#city',
                toolbar: {
                    show: true,
                },
                filterable: true,
                paging: true,
                pageSize: 10,
                data: [],
                radio: true,
                clickClose: true,
                tips: '请选择省',
                name: 'city',
                on: function (data) {
                    let arr = data.arr,
                        isAdd = data.isAdd;
                    if (isAdd) {
                        $.get("/admin/system.area/getCityByAreaNameAndValue", {
                            city: arr[0].value
                        }).then(res => {
                            if (res.code == 1) {
                                area.update({
                                    data: res.data
                                })
                            }
                        })
                    }
                }
            })
            let area = xmSelect.render({
                el: '#area',
                toolbar: {
                    show: true,
                },
                filterable: true,
                paging: true,
                pageSize: 10,
                data: [],
                radio: true,
                clickClose: true,
                tips: '请选择市',
                name: 'area',
                on: function (data) {
                    console.log(data)
                }
            })
            let address = xmSelect.render({
                el: '#address',
                toolbar: {
                    show: true,
                },
                filterable: true,
                paging: true,
                pageSize: 10,
                data: [],
                radio: true,
                clickClose: true,
                tips: '请输入搜索',
                name: 'address',
                remoteSearch: true,
                remoteMethod: function (val, cb, show) {
                    //这里如果val为空, 则不触发搜索
                    if (!val) {
                        return cb([]);
                    }
                    let obj_pro = xmSelect.get('#province', true);

                    //这里引入了一个第三方插件axios, 相当于$.ajax
                    $.get("/admin/mall.shop/getShopByAddress", {
                        keywords: val,
                        city: obj_pro.getValue('nameStr')
                    }).then(res => {
                        if (res.code === 1) {
                            console.log(res.data)
                            cb(res.data)
                        }
                    })
                },
                on: function (data) {
                    console.log(data)
                    let arr = data.arr,
                        isAdd = data.isAdd;
                    if (isAdd) {
                        if (arr.length != 0) {
                            $("#lat").val(arr[0].lat);
                            $("#lng").val(arr[0].lng);
                        }
                    }
                }

            })
            $.get("/admin/system.province/getProvinceByList").then(res => {
                if (res.code === 1) {
                    province.update({
                        data: res.data
                    })
                }
            })
            ea.listen();
        },
        machine: function () {
            ea.listen();
        },
        print: function () {
            ea.listen();
        }
    };
    return Controller;
});